home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacHack 1998
/
MacHack 1998.toast
/
The Hacks!
/
ReverseMouse
/
main.c
next >
Wrap
C/C++ Source or Header
|
1998-05-09
|
4KB
|
169 lines
// Program Author: Paul Baxter
// pbaxter@assistivetech.com
//
#include <DeskBus.h>
#include "ShowInit.h"
#ifndef powerc
#pragma error Sorry this only works on a real machine
#endif
// Control Flags....What do you want this hack to do?
#define REVERSE_DIRECTION 1
#define CHANGE_BUTTON_STATE 0
#define SWAP_AXIS 0
#define kMouseADBAddress 3
#define TwosComplement(n) ((~(n)) + 1)
#define kIconID 128
#define kNoInstallID 129
#define kButtonStateMask 0x80
#define kValueMask 0x7F
// Where is this defined??
extern ADBInitUPP JADBProc : 0x06B8;
ADBInitUPP gOldADBInitProc;
ADBServiceRoutineUPP gMouseServiceRoutine = nil;
ADBServiceRoutineUPP gOldMouseServiceRoutine = nil;
ADBDataBlock gMouseADBinfo;
extern Boolean InstallServiceRoutines(void);
extern void InstallJADBProc(void);
extern void MyADBInitProc(SInt8 callOrder);
extern pascal void ADBMouseServiceRoutine(Ptr buffer, TempADBServiceRoutineUPP completionProc, long refCon, long command);
#ifdef powerc
// for Metrowerks' linker, this defines the interface for main().
ProcInfoType __procinfo = kCStackBased | RESULT_SIZE(kNoByteCode);
#endif
// Main entry point
void main(void)
{
Handle ourhandle;
Boolean InstalledOK = false;
THz pZone;
pZone = GetZone();
SetZone(SystemZone());
ourhandle = Get1IndResource('INIT', 1);
if (ourhandle) {
DetachResource(ourhandle);
HLock(ourhandle);
HNoPurge(ourhandle);
// take over the ADBServiceRoutine for the Mouse
InstalledOK = InstallServiceRoutines();
if (InstalledOK) {
// Install a JADBProc so we don't go away after a ADBReInit
InstallJADBProc();
}
}
if (InstalledOK)
ShowINIT(kIconID, -1);
else
ShowINIT(kNoInstallID, -1);
SetZone(pZone);
}
// Setup our service routines
Boolean InstallServiceRoutines(void)
{
long adbaddr;
ADBDataBlock adb_data;
ADBSetInfoBlock setADBInfo;
short adbcount, adbindex;
OSErr err;
err = -1;
adbcount = CountADBs();
for (adbindex = 1; adbindex <= adbcount; adbindex++) {
adbaddr = GetIndADB(&adb_data,adbindex);
if ((adb_data.origADBAddr == kMouseADBAddress)) {
BlockMoveData(&adb_data, &gMouseADBinfo, sizeof(ADBDataBlock));
// Remember we will be called once to install and twice during each ADBReInit
if (!gMouseServiceRoutine) {
gMouseServiceRoutine = NewADBServiceRoutineProc(ADBMouseServiceRoutine);
}
if (!gMouseServiceRoutine)
return err == noErr;
gOldMouseServiceRoutine = adb_data.dbServiceRtPtr;
setADBInfo.siService = gMouseServiceRoutine;
setADBInfo.siDataAreaAddr = adb_data.dbDataAreaAddr;
err = SetADBInfo(&setADBInfo, adbaddr);
break;
}
}
return err == noErr;
}
// Install a JADBProc callback for before and after ADBReInit
void InstallJADBProc(void)
{
ADBInitUPP ourADBInitProc;
gOldADBInitProc = JADBProc;
ourADBInitProc = NewADBInitProc(MyADBInitProc);
JADBProc = ourADBInitProc;
}
// Handler for Mouse service routine
pascal void ADBMouseServiceRoutine(Ptr buffer, TempADBServiceRoutineUPP completionProc, long refCon, long command)
{
#pragma unused (refCon)
unsigned char num, buttonstate, count;
for (count = 0; count < buffer[0]; count++) {
// high most bit is the button state
// get the x and y data and take the 2's complement to reverse direction
buttonstate = buffer[count + 1] & kButtonStateMask;
num = buffer[count + 1] & kValueMask;
#if REVERSE_DIRECTION
// swap left right and up down
num = TwosComplement(num);
#endif
buffer[count + 1] = (num & kValueMask) | (buttonstate & kButtonStateMask);
}
#if CHANGE_BUTTON_STATE
// swap button up and button down
buffer[1] = (buffer[1] & kValueMask) | (~(buffer[1]) & kButtonStateMask);
#endif
#if SWAP_AXIS
// swap x and y axis
for (count = 1; count < buffer[0]; count++) {
unsigned char temp, temp2;
temp = buffer[count];
temp2 = buffer[count + 1];
buffer[count] = (temp2 & kValueMask) | (temp & kButtonStateMask);
buffer[count + 1] = (temp & kValueMask) | (temp2 & kButtonStateMask);
}
#endif
CallADBServiceRoutineProc(gOldMouseServiceRoutine, buffer, completionProc, (long)gMouseADBinfo.dbDataAreaAddr, command);
}
// needed to re-install our service routine
void MyADBInitProc(SInt8 callOrder)
{
Boolean InstallOK;
CallADBInitProc(gOldADBInitProc, callOrder);
if (callOrder)
InstallOK = InstallServiceRoutines();
}